Imports KanBan.Pgd.Presentation.Helpers
Imports KanBan.Pgd.Domain.Entities
Imports KanBan.Pgd.Presentation.Controllers
Imports KanBan.Pgd.Presentation.Views
Imports System.Resources
Imports KanBan.Pgd.Infrastructure.Repositories

Public Class ManageCandidates

    Public candidateSearchCollection As New AutoCompleteStringCollection()
    Private Const DefaulGridViewRowHeight As Integer = 36

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        SetPanelSizes()

        Dim repository As New CandidateRepository()

        Dim candidates As IEnumerable(Of Candidate) = repository.GetAll()

        Me.AddCandidates(candidates)

    End Sub

#Region "IManageCandidatesView"

    ''' <summary>
    ''' Agrega Candidatos a la grilla de Candidatos
    ''' </summary>
    ''' <param name="candidates"></param>
    ''' <remarks></remarks>
    Public Sub AddCandidates(ByVal candidates As IEnumerable(Of Candidate))

        For Each c As Candidate In candidates
            AddCandidate(c)
        Next

    End Sub


    ''' <summary>
    ''' Agrega un Candidato a la grilla de Candidatos
    ''' </summary>
    ''' <param name="candidate"></param>
    ''' <remarks></remarks>
    Public Sub AddCandidate(ByVal candidate As Candidate)

        Dim row As New DataGridViewRow()
        row.CreateCells(grvCandidates)
        row.Height = DefaulGridViewRowHeight

        row.Cells(GridColumns.Id).Value = candidate.Id
        row.Cells(GridColumns.Name).Value = candidate.Name
        row.Cells(GridColumns.Dni).Value = candidate.Dni
        row.Cells(GridColumns.Email).Value = candidate.Email

        grvCandidates.Rows.Add(row)

    End Sub

    ''' <summary>
    ''' Actualiza un Candidato de la lista de Candidatos
    ''' </summary>
    ''' <param name="candidate"></param>
    ''' <remarks></remarks>
    Public Sub UpdateCandidate(ByVal candidate As Candidate)

        For Each r As DataGridViewRow In grvCandidates.Rows
            If CType(r.Cells(GridColumns.Id).Value, Guid) = candidate.Id Then
                r.Cells(GridColumns.Name).Value = candidate.Name
                r.Cells(GridColumns.Dni).Value = candidate.Dni
                r.Cells(GridColumns.Email).Value = candidate.Email
                Exit For
            End If
        Next

    End Sub

    Public Sub ClearCandidates()

        grvCandidates.Rows.Clear()

    End Sub

    Public Sub SetCandidateAutocompleteResults(ByVal results As IList(Of String))

        For Each r As String In results
            candidateSearchCollection.Add(r)
        Next

        txtName.AutoCompleteMode = AutoCompleteMode.Suggest
        txtName.AutoCompleteSource = AutoCompleteSource.CustomSource
        txtName.AutoCompleteCustomSource = candidateSearchCollection

    End Sub

#End Region

#Region "Event Handling"

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click

        Me.AddNewCandidate()

    End Sub

    Public Sub AddNewCandidate()

        Dim form As New EditCandidate()
        AddHandler form.CandidateAdded, AddressOf OnCandidateAdded

        form.ShowDialog()

    End Sub

    Public Sub EditCandidate(ByVal candidateId As Guid)

        Dim form As New EditCandidate(candidateId)
        AddHandler form.CandidateUpdated, AddressOf OnCandidateUpdated

        form.ShowDialog()


    End Sub

    Private Sub OnCandidateAdded(ByVal candidate As Candidate)

        Me.AddCandidate(candidate)

    End Sub

    Private Sub OnCandidateUpdated(ByVal candidate As Candidate)

        Me.UpdateCandidate(candidate)

    End Sub

    Public Function DeleteCandidate(ByVal candidateId As Guid) As Boolean


        Dim candidateRepository As New CandidateRepository

        candidateRepository.DeleteLogical(candidateId)

        Return True

    End Function

    Public Sub ShowCandidate(ByVal candidateId As Guid)


        Dim form As New ShowCandidate(candidateId)

        form.ShowDialog()


    End Sub

    Public Sub Search(ByVal text As String)

        Dim repository As New CandidateRepository()

        Dim results As IEnumerable(Of Candidate) = repository.Query(text)

        Me.ClearCandidates()

        Me.AddCandidates(results)


    End Sub

    Public Sub UpdateCandidateAutocompleteResults(ByVal text As String)


        Dim repository As New CandidateRepository()

        Dim results As IList(Of String) = repository.QueryCandidates(text)

        Me.SetCandidateAutocompleteResults(results)


    End Sub

    Public Sub Search(ByVal name As String, ByVal dni As String)

        Dim repository As New CandidateRepository()

        Dim results As IEnumerable(Of Candidate) = repository.Query(name, dni)

        Me.ClearCandidates()

        Me.AddCandidates(results)


    End Sub

    Private Sub grvCandidates_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grvCandidates.CellContentClick

        If e.RowIndex >= 0 Then

            Dim candidateId As Guid = CType(grvCandidates.Rows(e.RowIndex).Cells(GridColumns.Id).Value, Guid)

            If e.ColumnIndex = GridColumns.Edit Then
                Me.EditCandidate(candidateId)
            End If

            Dim rm As New ResourceManager(GetType(ManageCandidates))

            If e.ColumnIndex = GridColumns.Delete Then
                If UIHelper.DeletionConfirmed(rm.GetString("ConfirmDeletionMessage")) Then
                    If (Me.DeleteCandidate(candidateId)) Then
                        grvCandidates.Rows.Remove(grvCandidates.Rows(e.RowIndex))
                    End If
                End If
            End If

            If e.ColumnIndex = GridColumns.Image Then
                Me.ShowCandidate(candidateId)
            End If

        End If

    End Sub

    Private Sub grvCandidates_CellMouseEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grvCandidates.CellMouseEnter
        If e.RowIndex >= 0 Then
            grvCandidates.Rows(e.RowIndex).Selected = True

            If e.ColumnIndex = GridColumns.Image Or e.ColumnIndex = GridColumns.Edit Or e.ColumnIndex = GridColumns.Delete Then
                Me.Cursor = Cursors.Hand
            End If

        End If
    End Sub

    Private Sub grvCandidates_CellMouseLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grvCandidates.CellMouseLeave
        If e.RowIndex >= 0 Then
            grvCandidates.Rows(e.RowIndex).Selected = False
            Me.Cursor = Cursors.Arrow
        End If
    End Sub

    Private Sub lnkSearchOptions_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkSearchOptions.LinkClicked

        If ContainerSearch.SplitterDistance = FormConstants.SearchPanelCollapsed Then
            btnSearch.Visible = False
            btnAdvancedSearch.Visible = True
            lnkSearchOptions.Text = "Simple Search"
            ContainerSearch.SplitterDistance = FormConstants.SearchPanelExpanded
        Else
            btnSearch.Visible = True
            btnAdvancedSearch.Visible = False
            lnkSearchOptions.Text = "Advanced Search"
            ContainerSearch.SplitterDistance = FormConstants.SearchPanelCollapsed
        End If

    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        Me.Search(txtName.Text)

    End Sub

    Private Sub btnAdvancedSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdvancedSearch.Click

        Me.Search(txtName.Text, _
                             txtDni.Text)


    End Sub

    Private Sub txtName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtName.TextChanged
        Me.UpdateCandidateAutocompleteResults(txtName.Text)
    End Sub

#End Region

#Region "Private"

    Private Enum GridColumns
        Image = 0
        Id = 1
        Name = 2
        Dni = 3
        Email = 4
        Edit = 5
        Delete = 6
    End Enum

    Private Sub SetPanelSizes()

        ContainerSearch.SplitterDistance = FormConstants.SearchPanelCollapsed

    End Sub

#End Region

End Class